/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:  Model class.
 */

package Decision_Lists;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;
import java.util.Vector;

/**
 */
public class DL_Model {

  private DL_ViewInterface curView;
  private String listName = "Houses to Buy";  // i.e., "Houses to Buy"
  private ArrayList <String> viewNames        = new ArrayList<String>();
  private ArrayList <String> viewNames_Center = new ArrayList<String>();
  private TreeMap <String, List_Item_Data> items_map = new TreeMap<String, List_Item_Data>(); // keyed on name
  private Vector <List_Item_Data>          items_vec = new Vector<List_Item_Data>();

  // observers is a list of Views that want to be notified of changes to the model
  private List<DL_ViewInterface> observers = new LinkedList<DL_ViewInterface>(); 


  ////// End Class Vars ////////
  //////////////////////////////

  
  // Constructor
  public DL_Model() {

  } //// end constructor /////


  // Adds a new list item
  public void addNewItem( List_Item_Data item )  {

    items_map.put(item.name, item);
    items_vec.add(item);

    // Notify all views of the change
    for (DL_ViewInterface view : observers) {
        view.itemAdded(item);
    }

  } /////// end addNewItem() ///////////////






  // Method adds a View to list of Observers to be notified of changes to the model
  public void addObserver(DL_ViewInterface view, String view_name, boolean is_center_view) {
    this.observers.add(view);
    this.viewNames.add(view_name);
    if(is_center_view) {
      this.viewNames_Center.add(view_name);
    }
    // second argument used to enforce/remind any new views that they new a "name" var.

  } /// end addObserver() //////





   ///// GETTERS and SETTERS ///////

  public void setCurView(DL_ViewInterface newView) {
    String old_view_name = this.curView.getViewName();
    this.curView = newView;
    // Notify all views of the change
    for (DL_ViewInterface view : observers) {
        view.curViewChanged(old_view_name, newView.getViewName());
    }
  } //////// end setValue() ///////////////

  public DL_ViewInterface getCurView() {
    return curView;
  }

  // Needed so that the initial view can be set after this model has been created
  //  (since the actual views had not yet been created when this model was)
  public void setInitialView(DL_ViewInterface initView) {

    this.curView = initView;
  } ///// end setInitialView() ////////

  public String getListName() {
    return listName;
  }

  ArrayList getViewNames() {
    return viewNames;
  }

  ArrayList getCenterViewNames() {
    return viewNames_Center;
  }

  public TreeMap<String, List_Item_Data> getItemsMap() {
    return items_map;
  }

  public Vector<List_Item_Data> getItemsVec() {
    return items_vec;
  }

  public List_Item_Data getCircularNextItem(int nextIndex) {
    int size = items_vec.size();

    // Make a negative number the positive equivelant
    while( nextIndex < 0 ) {
      nextIndex += size;
    }
    
    return items_vec.elementAt(nextIndex % size);
  }

  public List_Item_Data getCircularPrevItem(int prevIndex) {
    int size = items_vec.size();

    // Make a negative number the positive equivelant
    while( prevIndex < 0 ) {
      prevIndex += size;
    }

    return items_vec.elementAt(prevIndex % size);
  }
  

} /////////// end class DL_Model //////////////////////
